package com.atguigu.yygh.hosp.controller.admin;


import com.atguigu.yygh.exception.YyghException;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.result.R;
import com.atguigu.yygh.vo.hosp.HospitalSetQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 医院设置表 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2025-03-05
 */
@Api(tags = "管理员系统-医院设置")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController {
    @Autowired
    private HospitalSetService hospitalSetService;

    @ApiOperation(value = "查询所有医院设置列表")
    @GetMapping(path = "/findAll")
    public R findAll() {
//        try {
//            int i = 1/0;
//        }catch (Exception e){
//            throw new YyghException(20002, "测试异常");
//        }

        List<HospitalSet> list = hospitalSetService.list();

        return R.ok().data("list", list);
    }

    @ApiOperation(value = "根据id删除医院设置")
    @DeleteMapping(path = "/delete/{id}")
    public R delete(@ApiParam(name = "id", value = "医院设置id", required = true) @PathVariable(value = "id") Long id) {
        boolean flag = hospitalSetService.removeById(id);
        if (!flag){
            return R.error().message("删除失败");
        }else {
            return R.ok().message("删除成功");
        }
    }

    //带查询条件的分页查询
    @ApiOperation(value = "带查询条件的分页查询医院设置列表")
    @PostMapping(path = "/page/{current}/{size}")
    public R findPage(@PathVariable("current") Long current,
                      @PathVariable("size") Long size,
                      @RequestBody HospitalSetQueryVo hospitalSetQueryVo) {
        //构建查询条件
        Page<HospitalSet> page = new Page<>(current, size);
        QueryWrapper<HospitalSet> hospitalSetQueryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(hospitalSetQueryVo.getHosname())) {
            hospitalSetQueryWrapper.like("hosname", hospitalSetQueryVo.getHosname());
        }
        if (!StringUtils.isEmpty(hospitalSetQueryVo.getHoscode())) {
            hospitalSetQueryWrapper.like("hoscode", hospitalSetQueryVo.getHoscode());
        }

        //执行分页查询
        Page<HospitalSet> hospitalSetPage = hospitalSetService.page(page, hospitalSetQueryWrapper);
        return R.ok().data("total", hospitalSetPage.getTotal()).data("rows", hospitalSetPage.getRecords());
    }

    //新增医院设置
    @ApiOperation(value = "新增医院设置")
    @PostMapping(path = "/save")
    public R save(@RequestBody HospitalSet hospitalSet) {
        hospitalSetService.save(hospitalSet);
        return R.ok();
    }

    //查询医院设置详情
    @ApiOperation(value = "查询医院设置详情")
    @GetMapping(path = "/detail/{id}")
    public R detail(@ApiParam(name = "id", value = "医院设置id", required = true) @PathVariable(value = "id") Long id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return R.ok().data("item", hospitalSet);
    }

    //修改医院设置
    @ApiOperation(value = "修改医院设置")
    @PutMapping(path = "/update")
    public R update(@RequestBody HospitalSet hospitalSet) {
        hospitalSetService.updateById(hospitalSet);
        return R.ok();
    }
    //批量删除
    @ApiOperation(value = "批量删除医院设置")
    @DeleteMapping(path = "/batchRemove")
    public R batchRemove(@RequestBody List<Long> ids) {
        hospitalSetService.removeByIds(ids);
        return R.ok();
    }
    //修改状态
    @ApiOperation(value = "修改医院设置状态")
    @PutMapping(path = "/updateStatus/{id}/{status}")
    public R updateStatus(@ApiParam(name = "id", value = "医院设置id", required = true) @PathVariable(value = "id") Long id,
                          @ApiParam(name = "status", value = "状态", required = true) @PathVariable(value = "status") Integer status) {
        //根据id查询医院设置
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        //修改状态
        hospitalSet.setStatus(status);
        //更新状态
        hospitalSetService.updateById(hospitalSet);
        return R.ok();
    }
}

